﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Pomodoro
{
    public class ConfigForm : Form
    {
        private const string CONFIG_FILENAME = "config.txt";

        protected const int DEFAULT_ATOM_TIME = 25;
        protected const int DEFAULT_SHORT_BREAK_TIME = 5;
        protected const int DEFAULT_LONG_BREAK_TIME = 30;
        protected const int DEFAULT_ATOMS_BEFORE_LONG_BREAK = 4;

        private int _atomTime = -1;
        protected int AtomTime
        {
            get
            {
                if (_atomTime < 0)
                    LoadConfig();

                return _atomTime;
            }
            set
            {
                _atomTime = value;
            }
        }

        private int _shortBreakTime = -1;
        protected int ShortBreakTime
        {
            get
            {
                if (_shortBreakTime < 0)
                    LoadConfig();

                return _shortBreakTime;
            }
            set
            {
                _shortBreakTime = value;
            }
        }

        private int _longBreakTime = -1;
        protected int LongBreakTime
        {
            get
            {
                if (_longBreakTime < 0)
                    LoadConfig();

                return _longBreakTime;
            }
            set
            {
                _longBreakTime = value;
            }
        }

        private int _atomsBeforeLongBreak = -1;
        protected int AtomsBeforeLongBreak
        {
            get
            {
                if (_atomsBeforeLongBreak < 0)
                    LoadConfig();

                return _atomsBeforeLongBreak;
            }
            set
            {
                _atomsBeforeLongBreak = value;
            }
        }

        protected void LoadConfig()
        {
            AtomTime = DEFAULT_ATOM_TIME;
            ShortBreakTime = DEFAULT_SHORT_BREAK_TIME;
            LongBreakTime = DEFAULT_LONG_BREAK_TIME;
            AtomsBeforeLongBreak = DEFAULT_ATOMS_BEFORE_LONG_BREAK;

            if (!File.Exists(CONFIG_FILENAME))
                BuildConfigFile();

            if (!File.Exists(CONFIG_FILENAME))
                MessageBox.Show("Config file failed to load, using defaults");
            else
            {
                using (TextReader reader = new StreamReader(CONFIG_FILENAME))
                {
                    int cfg_atom_time = -1;
                    if (int.TryParse(reader.ReadLine().Trim(), out cfg_atom_time))
                    {
                        AtomTime = cfg_atom_time;
                    }
                    else
                    {
                        MessageBox.Show("Couldn't load Atom Time from config file, using default instead.");
                    }

                    int cfg_short_break_time = -1;
                    if (int.TryParse(reader.ReadLine().Trim(), out cfg_short_break_time))
                    {
                        ShortBreakTime = cfg_short_break_time;
                    }
                    else
                    {
                        MessageBox.Show("Couldn't load Short Break Time from config file, using default instead.");
                    }

                    int cfg_long_break_time = -1;
                    if (int.TryParse(reader.ReadLine().Trim(), out cfg_long_break_time))
                    {
                        LongBreakTime = cfg_long_break_time;
                    }
                    else
                    {
                        MessageBox.Show("Couldn't load Long Break Time from config file, using default instead.");
                    }

                    int cfg_atoms_before_long_break = -1;
                    if (int.TryParse(reader.ReadLine().Trim(), out cfg_atoms_before_long_break))
                    {
                        AtomsBeforeLongBreak = cfg_atoms_before_long_break;
                    }
                    else
                    {
                        MessageBox.Show("Couldn't load Atoms Before Long Break from config file, using default instead.");
                    }
                }
            }
        }

        private void BuildConfigFile()
        {
            SaveConfig();
        }

        protected void SaveConfig()
        {
            using (TextWriter writer = new StreamWriter(CONFIG_FILENAME))
            {
                writer.WriteLine(AtomTime);
                writer.WriteLine(ShortBreakTime);
                writer.WriteLine(LongBreakTime);
                writer.WriteLine(AtomsBeforeLongBreak);
            }
        }
    }
}
